package com.mind.adjust.service.impl;

import com.mind.adjust.mapper.AjAttachCateMapper;
import com.mind.adjust.mapper.AjRuleCheckMapper;
import com.mind.adjust.mapper.AjRuleDetailMapper;
import com.mind.adjust.service.AjRuleDetailService;
import com.mind.common.exception.ServiceException;
import com.mind.common.utils.StringUtils;
import com.mind.customer.mapper.AjObjectMapper;
import com.mind.customer.mapper.AjRuleMapper;
import com.mind.system.domain.AjObject;
import com.mind.system.domain.AjRule;
import com.mind.system.domain.AjRuleDetail;
import com.mind.system.domain.SysConfig;
import com.mind.system.mapper.SysConfigMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 规则明细(AjRuleDetail)表服务实现类
 *
 * @author makejava
 * @since 2024-07-11 20:40:13
 */
@Service("ruleDetailService")
public class AjRuleDetailServiceImpl implements AjRuleDetailService {
    @Resource
    private AjRuleMapper ruleMapper;
    @Resource
    private AjRuleDetailMapper ruleDetailMapper;

    @Resource
    private AjRuleCheckMapper ruleCheckMapper;

    @Resource
    private AjAttachCateMapper ajAttachCateMapper;
    @Resource
    private SysConfigMapper sysConfigMapper;
    @Resource
    private AjObjectMapper ajObjectMapper;

    /**
     * 查询规则明细信息集合
     *
     * @param ruleDetail 规则明细信息
     * @return 规则明细信息集合
     */
    @Override
    public List<AjRuleDetail> selectRuleDetailList(AjRuleDetail ruleDetail) {
        List<AjRuleDetail> ajRuleDetails = ruleDetailMapper.selectRuleDetailList(ruleDetail);
//        for (AjRuleDetail detail : ajRuleDetails) {
//            List<AjRule> rules = ruleMapper.selectByDetailId(detail.getId());
//            detail.setIsReference(!CollectionUtils.isEmpty(rules));
//            detail.setIsReference(false);
//        }
        return ajRuleDetails;
    }

    /**
     * 查询规则明细信息集合下拉框
     *
     * @param ruleDetail 查询规则明细信息集合下拉框
     * @return 规则明细信息集合
     */
    @Override
    public List<AjRuleDetail> selectRuleDetailListVo(AjRuleDetail ruleDetail) {

        return ruleDetailMapper.selectRuleDetailListVo(ruleDetail);
    }

    /**
     * 通过规则明细ID查询规则明细信息
     *
     * @param ruleDetailId 规则明细ID
     * @return 角色对象信息
     */
    @Override
    public AjRuleDetail selectRuleDetailById(Long ruleDetailId) {
        AjRuleDetail detail = ruleDetailMapper.selectRuleDetailById(ruleDetailId);
        List<AjRule> rules = ruleMapper.selectByDetailId(detail.getId());
//        detail.setIsReference(!CollectionUtils.isEmpty(rules));
        detail.setIsReference(false);
        return detail;
    }

    /**
     * 删除规则明细信息
     *
     * @param ruleDetailId 规则明细ID
     * @return 结果
     */
    @Override
    public int deleteRuleDetailById(Long ruleDetailId) {
        return ruleDetailMapper.deleteRuleDetailById(ruleDetailId);
    }

    /**
     * 批量删除规则明细信息
     *
     * @param ruleDetailIds 需要删除的规则明细ID
     * @return 结果
     */
    @Override
    public int deleteRuleDetailByIds(Long[] ruleDetailIds) {
        int i = ruleDetailMapper.deleteRuleDetailByIds(ruleDetailIds);
        for (Long id : ruleDetailIds) {
            ruleMapper.selectByDetailId(id.intValue()).forEach(rule -> {
                ruleMapper.deleteRuleById(rule.getId().longValue()
                );
            });
        }
        return i;
    }

    /**
     * 新增保存规则明细信息
     *
     * @param ruleDetail 规则明细信息
     * @return 结果
     */
    @Override
    public int insertRuleDetail(AjRuleDetail ruleDetail) {
        ruleDetail.setCode(ruleDetail.getAttachCate());
        return ruleDetailMapper.insertRuleDetail(ruleDetail);
    }

    /**
     * 生成规则编码
     *
     * @param ruleDetail
     * @return
     */
    private String getCode(AjRuleDetail ruleDetail) {
        AjObject ajObject = ajObjectMapper.selectObjectByName2(ruleDetail.getAttachCate());
        String code = "CS";

        /*
        AjAttachCate 表弃用了
        AjAttachCate attachCate = ajAttachCateMapper.selectAttachCateByName(ruleDetail.getAttachCate());
        if(StringUtils.isNotEmpty(attachCate.getCode())){
            code = attachCate.getCode();
        }*/
        if (ajObject != null && StringUtils.isNotEmpty(ajObject.getAttachCate())) {
            code = ajObject.getAttachCate();
        }
        String detailCode = ruleDetailMapper.selectLastByCate(ruleDetail.getAttachCate());
        Integer lastNum = 0;
        if (StringUtils.isNotEmpty(detailCode)) {
            Pattern pattern = Pattern.compile("\\d+");
            Matcher matcher = pattern.matcher(detailCode);
            if (matcher.find()) {
                String number = matcher.group();
                lastNum = Integer.parseInt(number);
            }
        }
        lastNum += 1;
        return String.format(code + "-%08d", lastNum);
    }

    /**
     * 修改保存规则明细信息
     *
     * @param ruleDetail 规则明细信息
     * @return 结果
     */
    @Override
    public int updateRuleDetail(AjRuleDetail ruleDetail) {
        SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique("checkEnable");
        if ("1".equals(sysConfig.getConfigValue())) {
            List<AjRule> rules = ruleMapper.selectByDetailId(ruleDetail.getId());
            if (!CollectionUtils.isEmpty(rules)) {
                for (AjRule rule : rules) {
                    if (!"3".equals(rule.getStatus())) {
                        throw new ServiceException("该规则明细已经关联含有非新增状态的规则配置，不能修改");
                    }
                }
            }
        }
        int i = ruleDetailMapper.updateRuleDetail(ruleDetail);
        if (i > 0) {
            ruleMapper.selectByDetailId(ruleDetail.getId()).forEach(rule -> {
                ruleMapper.updateRule(new AjRule() {{
                    setId(rule.getId());
                    setRuleDetailName(ruleDetail.getName());
                }});
            });

        }
        return i;
    }

    /**
     * 批量插入
     *
     * @param ruleDetailList 附件类型信息
     * @return 结果
     */
    @Override
    public int batchInsert(List<AjRuleDetail> ruleDetailList) {
        int size = 0;
        for (AjRuleDetail ruleDetail : ruleDetailList) {
            if (ruleDetailMapper.selectRuleDetailByName(ruleDetail.getName()) == null) {
                ruleDetailMapper.insertRuleDetail(ruleDetail);
                size++;
            }
        }
        return size;
    }

    /**
     * 复制明细
     *
     * @param ruleDetail
     * @return
     */
    @Override
    public int copy(AjRuleDetail ruleDetail) {
        if (StringUtils.isNull(ruleDetail.getId())) {
            throw new ServiceException("规则明细ID不能为空");
        }
        AjRuleDetail oldDetail = ruleDetailMapper.selectRuleDetailById(ruleDetail.getId().longValue());
        if (oldDetail == null) {
            throw new ServiceException("原有规则明细不存在");
        }
        oldDetail.setId(null);
        oldDetail.setName(oldDetail.getName() + "_copy");
        oldDetail.setCode(getCode(oldDetail));
        return ruleDetailMapper.insertRuleDetail(oldDetail);
    }
}
